package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.mapper.RegionMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RegionServiceImpl implements IRegionService {

    @Autowired
    private RegionMapper regionMapper;

    @Override
    public List<Region> selectForList(Long parentId) {

        return regionMapper.selectAll(parentId);
    }



    @Override
    public List<Map> SelectAllRegions(Long parentId) {

        List<Region> list = regionMapper.selectAll(parentId);
        List<Map> mapList = new ArrayList<Map>();
        for (Region r : list) {
            Map<String, Object> parentMap = r.toMap();
            List<Region> childList = regionMapper.selectAll(r.getId());
            if (childList!=null) {
                ArrayList<Map> objects = new ArrayList<>();
                for (Region region : childList) {
                    objects.add(region.toMap());
                }
                parentMap.put("nodes",objects);
            }
            mapList.add(parentMap);
        }
        System.out.println(mapList.toString());
        return mapList;
    }

    @Override
    public List<Region> query() {
        return regionMapper.selectForList();
    }

    @Override
    public void saveOrUpdate(Region region) {
        if (region.getId()==null) {
            regionMapper.insert(region);
        }else{
            regionMapper.updateByPrimaryKey(region);
        }
    }

    @Override
    public List<Region> selectCommend(Integer state) {

        return regionMapper.selectCommend(state);
    }
}
